{% extends 'base.html' %}

{% block title %}Create User{% endblock %}
{% block body %}
    {% for u in permissions %}
        <form class="layui-form layui-form-pane" action="">
            {% csrf_token %}
            <div class="layui-form-item ">
                <label class="layui-form-label">ID</label>
                <div class="layui-input-inline">
                    <input type="text" name="ID" required lay-verify="required"
                           autocomplete="off" class="layui-input" value="{{ u.id }}" onfocus="this.blur()"
                           style="background-color: #d9d9d9; cursor: not-allowed;">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">许可名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" required lay-verify="required" placeholder="输入许可名称"
                           autocomplete="off" class="layui-input" value="{{ u.name }}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">url</label>
                <div class="layui-input-inline">
                    <input type="text" name="url" required lay-verify="text" placeholder="输入url"
                           autocomplete="off" class="layui-input" value="{{ u.url_name }}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">method</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="method[GET]" lay-skin="tag" title="GET" {% if 'GET' in u.method %} checked {% endif %}>
                    <input type="checkbox" name="method[POST]" lay-skin="tag" title="POST" {% if 'POST' in u.method %} checked {% endif %}>
                    <input type="checkbox" name="method[DELETE]" lay-skin="tag" title="DELETE" {% if 'DELETE' in u.method %} checked {% endif %}>
                    <input type="checkbox" name="method[PUT]" lay-skin="tag" title="PUT" {% if 'PUT' in u.method %} checked {% endif %}>
                    <input type="checkbox" name="method[PATCH]" lay-skin="tag" title="PATCH"{% if 'PATCH' in u.method %} checked {% endif %} >
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">是否为菜单</label>
                <div class="layui-input-block">
                    <input type="checkbox" {% if u.is_menu %}checked ="" {% endif %} name="is_menu"
                           lay-skin="switch" lay-filter="switchTest" lay-text="是|否">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">父级菜单</label>
                <div class="layui-input-inline">
                    <select name="parent" lay-filter="menu">
                        <option value=""></option>
                        {% for m in menu %}
                            <option value="{{ m.id }}" {% if m.id == u.id %}selected{% endif %}>{{ m.name }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>

            <div class="layui-form-item" style="margin-top: 1%;">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="editPermission">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    {% endfor %}
{% endblock %}
{% block js %}
    <script>
        layui.use(['jquery', 'form', 'layer'], function () {
            var form = layui.form
                , $ = layui.jquery
                , layer = layui.layer;

            form.on('submit(editPermission)', function (data) {
                data = data.field;
                const csrf_token = $('[name="csrfmiddlewaretoken"]').val();
                data['csrfmiddlewaretoken'] = csrf_token;
                $.ajax({
                    url: "{% url 'auth:permission_api' %}",
                    type: 'PUT',
                    data: data,
                    dataType: 'json',
                    headers: {"X-CSRFToken": csrf_token},
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg(res.msg, {icon: 6});
                            document.location.href = "{% url 'auth:permission' %}", true;
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText, {icon: 5});
                    }
                })
                return false;
            });

        });
    </script>
{% endblock %}